<template>
  <div class="login-container">

    <div class="form-container">
      <h2>注册</h2>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
        <el-form-item prop="username">
          <el-input v-model="registerForm.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="registerForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码"></el-input>
        </el-form-item>
        <el-button type="primary" class="submit-btn" @click="handleRegister">注册</el-button>
        <div class="register-link">
          <span>已有账号？</span>
          <el-link type="primary" @click="goToLogin">立即登录</el-link>
        </div>
      </el-form>
    </div>

    <div class="welcome-container">
      <h1>欢迎加入<br/>GarbageRec!</h1>
      <div class="welcome-text">
        <p>与我们一起，共同推动环保事业</p>
        <p>让每一份努力都为地球增色</p>
        <p>共同打造可持续发展的未来</p>
        <p>回收之路，与你同行</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const registerFormRef = ref(null)

const registerForm = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePass2, trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  await registerFormRef.value.validate((valid) => {
    if (valid) {
      // TODO: 实现注册逻辑
      console.log('注册表单提交', registerForm)
    }
  })
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.login-container {
  display: flex;
  min-height: 79.9vh;
  background: linear-gradient(135deg, #1e90ff 0%, #67C23A 100%);
  align-items: center;
  justify-content: center;
  padding: 2rem;
  gap: 0;
}

.form-container,
.welcome-container {
  flex: 0 1 400px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 2rem;
  min-height: 400px;
}

.form-container {
  background: white;
  border-radius: 10px 0 0 10px;
  animation: fadeInLeft 0.8s ease-out forwards;
}

@keyframes fadeInLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.welcome-container {
  color: white;
  background: linear-gradient(135deg, #3aa1ad 0%, rgb(148.6, 212.3, 117.1) 100%);
  border-radius: 0 10px 10px 0;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  animation: fadeInRight 0.8s ease-out forwards;
}

@keyframes fadeInRight {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

h2 {
  margin-bottom: 2rem;
  font-size: 1.8rem;
  color: #333;
}

.el-form {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.submit-btn {
  width: 100%;
  margin-bottom: 1rem;
  height: 40px;
}

.register-link {
  text-align: center;
  margin-top: 1rem;
}

.welcome-container h1 {
  font-size: 2.5rem;
  margin-bottom: 2rem;
  opacity: 0;
  animation: fadeIn 0.8s ease-out 0.4s forwards;
}

.welcome-text p {
  margin: 0.5rem 0;
  font-size: 1.1rem;
  opacity: 0;
  animation: fadeIn 0.8s ease-out forwards;
}

.welcome-text p:nth-child(1) { animation-delay: 0.6s; }
.welcome-text p:nth-child(2) { animation-delay: 0.8s; }
.welcome-text p:nth-child(3) { animation-delay: 1.0s; }
.welcome-text p:nth-child(4) { animation-delay: 1.2s; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 0.9; }
}

@media (max-width: 768px) {
  .login-container {
    flex-direction: column;
    padding: 1rem;
  }

  .form-container,
  .welcome-container {
    flex: 0 1 auto;
    width: 100%;
    max-width: 100%;
    min-height: 400px;
  }

  .form-container {
    max-width: 100%;
    border-radius: 10px 10px 0 0;
  }

  .welcome-container {
    max-width: 100%;
    border-radius: 0 0 10px 10px;
    padding: 2rem;
  }
}
</style>